package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.NewsOne;
import com.langhua.service.NewsOneService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/newsOnes")
public class NewsOneController {


    @Autowired
    private NewsOneService newsOneService;


    /**
     * newsOne信息展示
     * @param current  当前页
     * @param size     每页条数
     * @param newsOne   条件查询
     * @return
     */
    @GetMapping("{current}/{size}")
    public R<IPage> getPage(@PathVariable Integer current, @PathVariable Integer size, NewsOne newsOne){

        IPage page=new Page(current,size);
        LambdaQueryWrapper<NewsOne> lqw=new LambdaQueryWrapper<NewsOne>();
        lqw.like(Strings.isNotEmpty(newsOne.getNewTitle()),NewsOne::getNewTitle,newsOne.getNewTitle());

        IPage page1 = newsOneService.page(page,lqw);
        System.out.println("newsOne页查询内容："+page1);
        return R.success(page1,"newsOne分页查询");
    }

    /**
     * newsOne查询全部信息
     * @return
     */
    @GetMapping("/getAllnewsOne")
    public R<List<NewsOne>> getAllnewsOne(){

        List<NewsOne> list = newsOneService.list();
        System.out.println("newsOne查询全部容："+list);
        return R.success(list,"newsOne查询全");
    }
    /**
     * newsOne查询推荐内容
     * @return
     */
    @GetMapping("/getByIsRecommendNewsOne")
    public R<List<NewsOne>> getByIsRecommendNewsOne(){

        LambdaQueryWrapper<NewsOne> lqw= new LambdaQueryWrapper<>();
        lqw.eq(NewsOne::getIsRecommend,"是");

        List<NewsOne> list = newsOneService.list(lqw);
        System.out.println("newsOne查询推荐内容："+list);
        return R.success(list,"newsOne查询推荐内容");
    }


    /**
     * 新增新闻1
     * @param newsOne
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody NewsOne newsOne){
        log.info("新增新闻1，新闻1信息：{}",newsOne.toString());

        System.out.println("新增得id："+newsOne);
        newsOneService.save(newsOne);

        return R.success(null,"新增新闻1成功");
    }


    /**
     * 根据id查询新闻1信息 (修改数据回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<NewsOne> getById(@PathVariable Long id){
        log.info("根据id查询新闻1信息..."+id);
        NewsOne newsOne = newsOneService.getById(id);
        if(newsOne != null){
            return R.success(newsOne,null);
        }
        return R.error("没有查询到对应新闻1信息");
    }

    /**
     * 根据id修改新闻1信息()
     * @param newsOne
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody NewsOne newsOne){
        log.info(newsOne.toString());

        long id = Thread.currentThread().getId();
        log.info("修改id为：{}",id);
        //Long empId = (Long)request.getSession().getAttribute("newsOne");
        //newsOne.setUpdateTime(LocalDateTime.now());
        //newsOne.setUpdateUser(empId);

        newsOneService.updateById(newsOne);

        return R.success(null,"新闻1信息修改成功");
    }

    /**
     * 通过id删除newsOne
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除newsOne的id为：{}", id);

        newsOneService.removeById(id);
        return R.success(null,"删除成功！");
    }

}
